package com.zhulin.android.edoctor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.zhulin.android.edoctor.db.DBDoctorDBHelp;
import com.zhulin.android.edoctor.jsonobject.JOCaseInfoModel;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class SplashActivity extends OrmLiteBaseActivity<DBDoctorDBHelp>{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash_content);
		
		

		new Thread(new Runnable() {
			
			@Override
			public void run() {
				copyDB();
			}
		}).start();
		
		handler.sendEmptyMessageDelayed(0,2000);
	}

	/**
	 * 将数据库拷出
	 */
	private void copyDB(){
		String strPackageName=getPackageName();
		String filePath="/data/data/"+strPackageName+"/databases/"+DBDoctorDBHelp.DB_Name_Hospital;
		InputStream insInputStream= this.getResources().openRawResource(R.raw.ehospital);
		FileOutputStream fos;
		try {
			
			
			File file=new File(filePath); 
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			
			if(file.exists()){
				file.delete();
			}
			fos = new FileOutputStream(filePath);
			byte[] buffer = new byte[8192];
			int count = 0;
			while ((count = insInputStream.read(buffer)) > 0)
			{
				fos.write(buffer, 0, count);
			}
			fos.close();
			insInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			handler.sendEmptyMessageDelayed(0,2000);
		}
	}

	private Handler handler=new Handler(new android.os.Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			startActivity(new Intent(SplashActivity.this, MainActivity.class));
			finish();
			return false;
		}
	});
}
